//
//  JpDate.m
//  yiyong
//
//  Created by apple on 15/5/30.
//  Copyright (c) 2015年 wangjinping. All rights reserved.
//

#import "JPDate.h"

@implementation JPDate

+(NSDateComponents *) dateComponents:(int) unitFlags withDate:(NSDate* ) date {
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:date];
    return comps;
}

+(NSInteger) year:(double) currentMillons {
    return [self dateComponents:NSCalendarUnitYear withDate:[NSDate dateWithTimeIntervalSince1970:currentMillons]].year;
}
+(NSInteger) month:(double) currentMillons {
    return [self dateComponents:NSCalendarUnitMonth withDate:[NSDate dateWithTimeIntervalSince1970:currentMillons]].month;
}

+(NSInteger) day:(double) currentMillons {
    return [self dateComponents:NSCalendarUnitDay withDate:[NSDate dateWithTimeIntervalSince1970:currentMillons]].day;
}

+(NSInteger) hour:(double) currentMillons {
    return [self dateComponents:NSCalendarUnitHour withDate:[NSDate dateWithTimeIntervalSince1970:currentMillons]].hour;
}

+(NSInteger) minute:(double) currentMillons {
    return [self dateComponents:NSCalendarUnitMinute withDate:[NSDate dateWithTimeIntervalSince1970:currentMillons]].minute;
}

+(NSInteger) second:(double) currentMillons {
    return [self dateComponents:NSCalendarUnitSecond withDate:[NSDate dateWithTimeIntervalSince1970:currentMillons]].second;
}

+(NSInteger) weekday:(double) currentMillons {
    return [self dateComponents:NSCalendarUnitWeekday withDate:[NSDate dateWithTimeIntervalSince1970:currentMillons]].weekday;
}

+(NSString *) weekday:(double) currentMillons withNumber:(BOOL) isNumber {
    return [self Weekday:[NSDate dateWithTimeIntervalSince1970:currentMillons] withNumber:isNumber];
}

+(NSString*) date:(double) currentMillons {
    return [JPDate date:currentMillons withSeparator:@"-"];
}

+(NSString*) date:(double) currentMillons withSeparator:(NSString *) separator{
    return [self date:currentMillons withSeparator:separator withTag:0];
}

+(NSString*) date:(double) currentMillons withSeparator:(NSString *) separator withTag:(int) tag {
    return [self Date:[NSDate dateWithTimeIntervalSince1970:currentMillons] withSeparator:separator withTag:tag];
}

+(NSInteger) Year:(NSDate *) date {
    return [self dateComponents:NSCalendarUnitYear withDate:date].weekday;
}

+(NSInteger) Month:(NSDate *) date {
    return [self dateComponents:NSCalendarUnitMonth withDate:date].month;
}

+(NSInteger) Day:(NSDate *) date {
    return [self dateComponents:NSCalendarUnitDay withDate:date].day;
}

+(NSInteger) Hour:(NSDate *) date {
    return [self dateComponents:NSCalendarUnitHour withDate:date].hour;
}

+(NSInteger) Minute:(NSDate *) date {
    return [self dateComponents:NSCalendarUnitMinute withDate:date].minute;
}

+(NSInteger) Second:(NSDate *) date {
    return [self dateComponents:NSCalendarUnitSecond withDate:date].second;
}

+(NSInteger) Weekday:(NSDate *) date {
    return [self dateComponents:NSCalendarUnitWeekday withDate:date].weekday;
}

+(NSString *) Weekday:(NSDate *) date withNumber:(BOOL) isNumber {
    if (isNumber) {
        return [NSString stringWithFormat:@"%ld",[JPDate Weekday:date]];
    } else {
        NSInteger weekday = [JPDate Weekday:date];
        switch (weekday) {
            case 1:
            return @"周日";
            case 2:
            return @"周一";
            case 3:
            return @"周二";
            case 4:
            return @"周三";
            case 5:
            return @"周四";
            case 6:
            return @"周五";
            case 7:
            return @"周六";
            default:
            return @"";
        }
        //        switch (weekday) {
        //            case 1:
        //                return @"Mon";
        //            case 2:
        //                return @"Tue";
        //            case 3:
        //                return @"Wed";
        //            case 4:
        //                return @"Tur";
        //            case 5:
        //                return @"Fri";
        //            case 6:
        //                return @"Sat";
        //            case 7:
        //                return @"Sun";
        //            default:
        //                return @"";
        //        }
    }
}

+(NSString*) Date:(NSDate *) date{
    return [JPDate Date:date withSeparator:@"-"];
}

+(NSString*) Date:(NSDate *) date withSeparator:(NSString *) separator {
    return [self Date:date withSeparator:separator withTag:0];
}

+(NSString*) Date:(NSDate *) date withSeparator:(NSString *) separator withTag:(int) tag {
    NSDateComponents *components = [self dateComponents:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute |NSCalendarUnitSecond withDate:date];
    NSInteger year = components.year;
    NSInteger month = components.month;
    NSInteger day = components.day;
    NSString *_date;
    if (tag == 0) {
        _date = [NSString stringWithFormat:@"%ld%@%ld%@%ld",year,separator,month,separator,day];
    } else {
        _date = [NSString stringWithFormat:@"%ld%@%@%@%@",
                 year,
                 separator,
                 month<10?[NSString stringWithFormat:@"0%ld",month]:[NSString stringWithFormat:@"%ld",month],
                 separator,
                 day<10?[NSString stringWithFormat:@"0%ld",day]:[NSString stringWithFormat:@"%ld",day]];
    }
    NSInteger hour = components.hour;
    NSInteger minute = components.minute;
    NSInteger second = components.second;
    NSString *time = [NSString stringWithFormat:@"%@:%@:%@",hour<10?[NSString stringWithFormat:@"0%ld",hour]:[NSString stringWithFormat:@"%ld",hour],
                      minute<10?[NSString stringWithFormat:@"0%ld",minute]:[NSString stringWithFormat:@"%ld",minute],
                      second<10?[NSString stringWithFormat:@"0%ld",second]:[NSString stringWithFormat:@"%ld",second]];
    return [NSString stringWithFormat:@"%@ %@", _date, time];
}



+(NSDate*)TimeZoneChange{
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:date];
    return [date dateByAddingTimeInterval: interval];
}

+(long long) currentTimeMillis {
    return [[NSDate date] timeIntervalSince1970]*1000;
}

+(long long) timeMillisBeforeSeconds:(long long)seconds {
    long long current = [JPDate currentTimeMillis];
    return current-seconds*1000;
}

+(long long) timeMillisAfterSeconds:(long long)seconds {
    long long current = [JPDate currentTimeMillis];
    return current+seconds*1000;
}

+ (int)getAge:(long long) timeMillion {
    
    if (timeMillion > [JPDate currentTimeMillis]) {
//        [NSException exceptionWithName:@"IllegalArgumentException" reason:@"出生年月应该在当前时间之前" userInfo:nil];
        return 0;
    }
    
    NSDateComponents *component = [JPDate dateComponents:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay withDate:[NSDate date]];
    
    NSInteger yearNow       = component.year;
    NSInteger monthNow      = component.month;
    NSInteger dayOfMonthNow = component.day;
    
    component = [JPDate dateComponents:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay withDate:[NSDate dateWithTimeIntervalSince1970:timeMillion/1000.0]];
    
    NSInteger yearBirth       = component.year;
    NSInteger monthBirth      = component.month;
    NSInteger dayOfMonthBirth = component.day;
    
    NSInteger age = yearNow - yearBirth;
    if (monthNow <= monthBirth) {
        if (monthNow == monthBirth) {
            if (dayOfMonthNow < dayOfMonthBirth) {
                age--;
            }
        } else {
            age--;
        }
    }
    
    //可能系统日期设置错误 似乎好像ios日历不会初始化到以前的日历，android会出现这个问题
    NSInteger age2 = (int)(([JPDate currentTimeMillis]-timeMillion)/1000/(60*60*24*365));
    if (abs((int)(age2-age)) > 1) {
        age = age2;
    }
    
    return (int)age;
}


+ (NSString*)timeFormatFromSeconds:(NSInteger)seconds {
    //format of hour
    NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
    //format of minute
    NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
    //format of second
    NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
    //format of time
    NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
    return format_time;
}

+ (NSDate *)getNowDateFromDate:(NSDate *)anyDate {
    
    //设置源日期时区
    
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
    
    //设置转换后的目标日期时区
    
    NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
    
    //得到源日期与世界标准时间的偏移量
    
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];
    
    //目标日期与本地时区的偏移量
    
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];
    
    //得到时间偏移量的差值
    
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    
    //转为现在时间
    
    NSDate* destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate];
    
    return destinationDateNow;
    
}

+ (NSString *)dateToString:(long long)time{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time/1000.0];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [dateFormatter stringFromDate:date];
}

+ (NSString *)dateToString:(long long)time withFormat:(NSString *)format {
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time/1000.0];
    if (!format) {
        return [NSString stringWithFormat:@"%@", date];
    }
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    return [dateFormatter stringFromDate:date];
}

@end
